package com.baizhi.controller;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.CmfzAdmin;
import com.baizhi.service.CmfzAdminService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("admin")
public class CmfzAdminController {
    @Autowired
    private CmfzAdminService cmfzAdminService;
    @RequestMapping("selectAll")
    @ResponseBody
    @LogAnnotation(content = "查询所有管理员信息",type = "查询")
    public Map selectAll(Integer page,Integer limit){
        HashMap map = new HashMap();
        Page<CmfzAdmin> adminPage = cmfzAdminService.selectAll(page, limit);
        map.put("msg","");
        map.put("code",0);
        map.put("data",adminPage.getRecords());
        map.put("count",adminPage.getTotal());
        return map;
    }
    @RequestMapping("insert")
    @ResponseBody
    @LogAnnotation(content = "添加管理员",type = "添加")
    public Map insert(CmfzAdmin admin){
        HashMap map = new HashMap();
        try{
            int insert = cmfzAdminService.insert(admin);
            if (insert>0){
                map.put("flag",true);
            }else {
                map.put("flag",false);
            }
        }catch (Exception e){
            map.put("flag",false);
        }
        return map;
    }
    /*获取验证码的方法*/
    @RequestMapping("getCode")
    public void getCode(HttpSession session, HttpServletResponse response) throws IOException {
        /*生成验证码，并设置验证码的长和宽*/
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(200, 100);
        /*获取验证码的值*/
        System.out.println(captcha.getCode());
        /*将验证码放入session中*/
        session.setAttribute("realcode",captcha.getCode());
        /*将验证码图片响应到页面中*/
        captcha.write(response.getOutputStream());
    }
    @LogAnnotation(content = "管理员登录",type = "登录")
    @RequestMapping("login")
    public String login(String username, String password, String userprint, HttpSession session){
        String realcode= (String)session.getAttribute("realcode");
        System.out.println("realcode="+realcode);
        if(!(userprint!=null&&userprint.equalsIgnoreCase(realcode))) {
            System.out.println("验证码输入错误！");
            return "redirect:/login.jsp";
        }
        /*1.将页面用户输入的账号密码  封装到token中*/
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        /*2.获取Subject*/
        Subject subject = SecurityUtils.getSubject();
        /*3.使用Subject的认证方法完成认证*/
        try {
            subject.login(token);
            System.out.println("认证成功!");
            return "redirect:/main.jsp";
        }catch (Exception e){
            System.out.println("认证失败");
            return "redirect:/login.jsp";
        }
    }
    @ResponseBody
    @RequestMapping("register")
    public String register(String username,String password){
        System.out.println("账号："+username);
        System.out.println("密码："+password);
        Boolean register = cmfzAdminService.register(username,password);
        if (register){
            return "注册成功!";
        }else {
            return "注册失败!";
        }
    }
}
